package com.asa.common.widgets;

import android.animation.AnimatorSet;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.appcompat.widget.AppCompatButton;



/**
 * Author: wangchunhe
 * Create: 2018/5/25
 * Describe:
 */

public class AnimClickButton extends AppCompatButton {
    private AnimatorSet mDownAnim;
    private AnimatorSet mUpAnim;

    public AnimClickButton(Context context) {
        this(context, null);
    }

    public AnimClickButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AnimClickButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAnim();
    }

    private void initAnim() {
        mDownAnim = NewButtonAnim.loadDownAnim(this);
        mUpAnim = NewButtonAnim.loadUpAnim(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mDownAnim.start();
        } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
            mDownAnim.cancel();
            mUpAnim.start();
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mDownAnim.cancel();
        mUpAnim.cancel();
    }
}
